package cn.innosmart.aq.camera.models;

import android.app.Fragment;
import android.content.Context;
import android.graphics.Bitmap;

import com.tutk.IOTC.AVIOCTRLDEFs;
import com.tutk.ipcam.CameraDevice;
import com.tutk.ipcam.IpCamera;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import cn.innosmart.aq.camera.protocol.BaseError;
import cn.innosmart.aq.camera.protocol.Camera;
import cn.innosmart.aq.camera.protocol.CommandCompletionHandler;
import cn.innosmart.aq.camera.protocol.CommandError;

/**
 * Created by David on 2015/10/13.
 */
public class CameraBean implements Camera {


    private String id;
    private String uid;
    private String psd;
    private String title;
    private String place;
    private int status;
    private boolean isShown;
    private Object tag;
    private transient IpCamera mCameraDevice;


    public CameraBean() {
    }

    public CameraBean(JSONObject jsonObject) {
        try {
            this.id = jsonObject.getString("id");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            this.uid = jsonObject.getString("uid");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            this.psd = jsonObject.getString("password");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            this.title = jsonObject.getString("title");
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public boolean isShown() {
        return isShown;
    }

    public void setShown(boolean shown) {
        isShown = shown;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public Object getTag() {
        return tag;
    }

    public void setTag(Object tag) {
        this.tag = tag;
    }

    public String getPlace() {
        return place;
    }

    public void setPlace(String place) {
        this.place = place;
    }

    public void initCameraDevice(Context ctx) {
        mCameraDevice = new CameraDevice(ctx, title, uid, "admin", psd);
    }

    /**
     * 初始化摄像头的连接
     * @param ctx
     * @param onStreamInfoUpdate
     */
    public void initCameraDevice(Context ctx, OnStreamInfoUpdate onStreamInfoUpdate) {
        mCameraDevice = new CameraDevice(ctx, title, uid, "admin", psd);
        setOnStreamInfoUpdate(onStreamInfoUpdate);
    }


    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public String getPsd() {
        return psd;
    }

    public void setPsd(String psd) {
        this.psd = psd;
    }

    @Override
    public Fragment getView() {
        return mCameraDevice.getView();
    }

    @Override
    public Bitmap getSnapShot() {
        return mCameraDevice.getSnapshot();
    }

    @Override
    public void record(int sec, String path, final RecordingCallback lis) {

        mCameraDevice.record(sec, path, new IpCamera.RecordingCallback() {
            @Override
            public void onRecordStart() {
                lis.onRecordStart();
            }

            @Override
            public void onRecordFinished() {
                lis.onRecordFinished();
            }
        });
    }

    @Override
    public void listen() {
        mCameraDevice.listen();
    }

    @Override
    public void speak() {
        mCameraDevice.speak();
    }

    @Override
    public void mute() {
        mCameraDevice.mute();
    }

    @Override
    public void setOnStreamInfoUpdate(final OnStreamInfoUpdate lis) {
        mCameraDevice.setOnStreamInfoUpdate(new IpCamera.OnStreamInfoUpdate() {
            @Override
            public void onFrameRateUpdate(String frameRate) {
                lis.onFrameRateUpdate(frameRate);
            }

            @Override
            public void onFrameResolutionUpdate(String resolution) {
                lis.onFrameResolutionUpdate(resolution);
            }

            @Override
            public void onCameraConnected() {
                lis.onCameraConnected();
            }

            @Override
            public void onCameraStatus(String did, int status) {
                lis.onCameraStatus(did, status);
            }
        });
    }


    @Override
    public void reconnect() {
        if (mCameraDevice != null) {
            mCameraDevice.reInit(getTitle(), getUid(), getPsd());
            mCameraDevice.reconnect();
        }
    }

    @Override
    public void setPassword(String oldPassword, String newPassword, final CommandCompletionHandler<Integer> cb) {
        mCameraDevice.setPassword(oldPassword, newPassword, new IpCamera.AsyncCallback<Integer>() {

            @Override
            public void onSuccess(Integer integer) {
                cb.onCompletion(integer);
            }

            @Override
            public void onFailed() {
                cb.onFailed(new CommandError(new BaseError()));
            }
        });
    }

    @Override
    public void getVideoQuality(final CommandCompletionHandler<Integer> cb) {
        mCameraDevice.getVideoQuality(new IpCamera.AsyncCallback<Integer>() {
            @Override
            public void onSuccess(Integer integer) {
                cb.onCompletion(integer);
            }

            @Override
            public void onFailed() {
                cb.onFailed(new CommandError(new BaseError()));
            }
        });
    }


    @Override
    public void setVideoQuality(int videoQuality) {
        mCameraDevice.setVideoQuality(videoQuality);
    }


    @Override
    public void getVideoMode(final CommandCompletionHandler<Integer> cb) {
        mCameraDevice.getVideoMode(new IpCamera.AsyncCallback<Integer>() {
            @Override
            public void onSuccess(Integer mode) {
                cb.onCompletion(mode);
            }

            @Override
            public void onFailed() {

            }
        });
    }

    @Override
    public void setVideoMode(int mode) {
        mCameraDevice.setVideoMode(mode);
    }

    @Override
    public void getWifiList(final CommandCompletionHandler<ArrayList<AVIOCTRLDEFs.SWifiAp>> cb) {
        mCameraDevice.getWifiList(new IpCamera.AsyncCallback<ArrayList<AVIOCTRLDEFs.SWifiAp>>() {
            @Override
            public void onSuccess(ArrayList<AVIOCTRLDEFs.SWifiAp> list) {
                cb.onCompletion(list);
            }

            @Override
            public void onFailed() {

            }
        });
    }

    @Override
    public void setWifi(AVIOCTRLDEFs.SWifiAp sWifiAp, String password, final CommandCompletionHandler<Integer> cb) {
        mCameraDevice.setWifi(sWifiAp, password, new IpCamera.AsyncCallback<Integer>() {
            @Override
            public void onSuccess(Integer integer) {
                cb.onCompletion(integer);
            }

            @Override
            public void onFailed() {

            }
        });
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String toAddParam() {
        JSONObject jsParam = new JSONObject();
        try {
            jsParam.put("uid", uid);
            jsParam.put("title", title);
            jsParam.put("password", psd);
            jsParam.put("place", place);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsParam.toString();
    }

    public String toRemoveParam() {
        JSONObject jsParam = new JSONObject();
        try {
            jsParam.put("uid", uid);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsParam.toString();
    }

    public String toUpdateParam() {
        JSONObject jsParam = new JSONObject();
        try {
            jsParam.put("uid", uid);
            jsParam.put("title", title);
            jsParam.put("password", psd);
            jsParam.put("place", place);
            jsParam.put("id", id);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsParam.toString();
    }

    public void stopShow() {
        if(mCameraDevice != null)
            mCameraDevice.stopShow();
    }

    public String toChangePasswordParam(String password) {
        JSONObject jsParam = new JSONObject();
        try {
            jsParam.put("uid", uid);
            jsParam.put("password", password);
            jsParam.put("id", id);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsParam.toString();
    }

    public void disConnect() {
        if (mCameraDevice != null)
            mCameraDevice.disconnect();
    }

}
